﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Input;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPFManager
{
    [CPF.Design.DesignerLoadStyle("res://CPFManager/Stylesheet1.css")]//用于设计的时候加载样式
    public class BaseDialog : ContentControl
    {
        //模板定义
        protected override void InitializeComponent()
        {
            Height = 222;
            Width = 390;
            Background = "#fff";

            Children.Add(new Border
            {
                Name = "contentPresenter",
                Height = "100%",
                Width = "100%",
                BorderFill = null,
                PresenterFor = this
            });

            Children.Add(new Panel
            {
                Name = "close",
                ToolTip = "关闭",
                MarginRight = 5,
                MarginTop = 5,
                Width = 30,
                Height = 30,
                Children =
                {
                    new Line
                    {
                        MarginTop=8,
                        MarginLeft=8,
                        StartPoint = new Point(1, 1),
                        EndPoint = new Point(14, 13),
                        StrokeStyle = "2",
                        IsAntiAlias=true,
                    },
                    new Line
                    {
                        MarginTop=8,
                        MarginLeft=8,
                        StartPoint = new Point(14, 1),
                        EndPoint = new Point(1, 13),
                        StrokeStyle = "2",
                        IsAntiAlias=true,
                    }
                },
                Commands =
                {
                    {
                        nameof(Button.MouseDown),
                        (s, e) => CloseDialog()
                    }
                }
            });
        }

        public BaseDialog()
        {
            layer.Children.Add(this);
            layer.MouseDown += Layer_MouseDown;
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            e.Handled = true;
        }

        private void Layer_MouseDown(object sender, CPF.Input.MouseButtonEventArgs e)
        {
            if (Root is Window window)
            {
                window.DragMove();
            }
        }

        Panel layer = new Panel
        {
            Width = "100%",
            Height = "100%",
            Background = "0,0,0,0"
        };
        public void ShowDialog(View root)
        {
            root.Children.Add(layer);
            MarginTop = -100;
            this.TransitionValue(nameof(MarginTop), (FloatField)100, TimeSpan.FromSeconds(0.3), new PowerEase { }, AnimateMode.EaseOut);
            layer.TransitionValue(nameof(Background), (ViewFill)"0,0,0,100", TimeSpan.FromSeconds(0.3));
        }

        public void CloseDialog()
        {            
            //采用过渡属性的写法定义淡出效果
            layer.TransitionValue(nameof(Control.Background), (SolidColorFill)"0,0,0,0", TimeSpan.FromSeconds(0.3), null, AnimateMode.Linear, () =>
            {
                if (Root != null)
                {
                    Root.Children.Remove(layer);
                }
            });

            this.TransitionValue(nameof(MarginTop), (FloatField)(-100), TimeSpan.FromSeconds(0.3), new PowerEase { }, AnimateMode.EaseIn);
        }
    }
}
